<?php

class Tag extends DataObject
{
    // Declare data attributes
    protected $id;
    protected $parentid;                //int 
    protected $tag;
    protected $tagtype;                 //int
    
    protected $parent;

    
    public function fetchFromDB($id, $id2=null)
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        // Name of stored procedure
        $proc = 'TagFetch';
        $params = array($id);
        if ($db->executeStoredProcedure($proc, $params, $returnData)){
                
            $this->id = intval($id);
            $this->parentid = intval($returnData[0]["parent"]);
            $this->tag = $returnData[0]["tag"];
            $this->tagtype = intval($returnData[0]["tagtype"]);

            // Stored procedure succeeded, return true
            return true;
        }
        // Stored procedure failed, return false
        return false;
    }
    
    public function commit()
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        $proc = 'TagCommit';
        $params = Array(
            $this->id,
            $this->tag
            );
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function delete()
    {
        $db = DBConnection::inst(); 
        $proc = 'TagDelete';
        $params = array($this->id);
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function create()
    {
        if (! isset($this->id))
        {
            // No database object stored.
            $db = DBConnection::inst();
            $proc = 'TagCreate';
            $params = Array(
                $this->parentid,
                $this->tag,
                $this->tagtype,
            );
            
            // Sets the ID to the return value of the stored procedure, which
            // is the ID of the newly created objects.
            if ($this->id = $db->executeStoredProcedure($proc, $params, $returnData))
            {
                return true;
            }
            return false;
        }
        // data object already set
        return false;
    }
    
    public function link()
    {
        if (isset($this->parent)) return true;
        $DOM = DataObjectManager::inst();
        if (!isset($this->parentid))
            return false; 
        switch ($this->tagtype)
        {
            case PODCAST_TYPE:
                $this->parent = $DOM->fetchDataObject("Podcast", $this->parentid);
                break;
            case VIDEO_TYPE:
                $this->parent = $DOM->fetchDataObject("Video", $this->parentid);
                break;
            case NEWS_TYPES:
                $this->parent = $DOM->fetchDataObject("NewsPost", $this->parentid);
                break;
            default:
                return false;
        }
        $this->parent->link();
        return true;
    }
}

?>
